const LogModel = require('../../model/Log');
const moment = require('moment')
const Banner = require('../../model/Banner')

module.exports = {

    //获取日志列表
    changeBanner: async(req, res, next) => {
        try {
            let banner = await Banner.findOne({ page: req.body.page }).exec()
            if (banner == null) {
                let newBanner = new Banner({
                    page: req.body.page,
                    updatedAt: new Date(),
                    image: req.body.image, // link of cdn images
                    isDelete: 0,
                    refLink: req.body.refLink, // 小程序内跳转链接或者网页链接,
                })
                await newBanner.save()
                return res.ok('创建成功', newBanner)
            } else {
                banner.updatedAt = new Date()
                banner.image = req.body.image
                banner.refLink = req.body.refLink
                await banner.save()
                return res.ok('修改成功', banner)
            }

            // if (req.body.id) {
            //     let banner = await Banner.findOne({ _id: req.body.id }).exec()
            //     banner.updatedAt = new Date()
            //     banner.image = req.body.image
            //     banner.refLink = req.body.refLink
            //     await banner.save()
            //     return res.ok('修改成功', banner)
            // } else if (!req.body.id) {
            //     let newBanner = new Banner({
            //         updatedAt: new Date(),
            //         image: req.body.image, // link of cdn images
            //         isDelete: 0,
            //         refLink: req.body.refLink, // 小程序内跳转链接或者网页链接,
            //     })
            //     await newBanner.save()
            //     return res.ok('创建成功', newBanner)
            // }
        } catch (e) {
            next(e)
        }
    },

    deleteBanner: async(req, res, next) => {
        try {
            let banner = await Banner.findOne({ page: req.body.page }).exec()
            banner.isDelete = 1
            await banner.save()
            return res.ok('删除成功')
        } catch (e) {
            next(e)
        }
    },

    getBanner: async(req, res, next) => {
        try {
            let banner = await Banner.findOne({ page: req.body.page }).exec()
            return res.ok('查询成功', banner)
        } catch (e) {
            next(e)
        }
    },

};